package com.gitee.wsl.mathematics.function.equation

import com.gitee.wsl.ext.collection.uniqueOn

/**
 * Get the argument matrix
 * 获取系数矩阵
 */
val AugmentedMatrix.args get() = first

/**
 * Get the constant vector
 * 获取常数项
 */
val AugmentedMatrix.constants get() = second


/**
 * Check whether a set of equation is correct
 * 判断方程组是否合理
 * 即 其中至少有一个方程 且 其中各方程参数维数相同
 */
fun EquationSet.isAvailable() = uniqueOn { it.first.dim }

/**
 * Check whether a augmented matrix is correct
 * 判断增广矩阵是否合理
 * 即 系数矩阵行数 等于 常数向量维数
 */
fun AugmentedMatrix.isAvailable() = args.rowCount == constants.dim

/**
 * Check whether a equation set is under determined
 * 判断方程组是否欠定
 */
fun AugmentedMatrix.isUnderdetermined() = args.rowCount < args.colCount

/**
 * Check whether a equation set is well posed
 * 判断方程组是否适定
 */
fun AugmentedMatrix.isWellPosed() = args.isSquare

/**
 * Check whether a equation set is over determined
 * 判断方程组是否超定
 */
fun AugmentedMatrix.isOverdetermined() = args.rowCount > args.colCount


/**
 *  Display an augmented matrix
 * 方程组显示为增广矩阵
 */
/*fun AugmentedMatrix.matrixView(header: String = "${args.colCount} members equation set") =
    buildString {
        val lines =
            tie(args.matrixView(""),
                constants.toListMatrix().matrixView(""))
                .lines()
        val w0 = lines.map(String::length).max() ?: 0
        val w1 = max(header.length, w0)
        val indent = blank((w1 - w0) / 2)

        append(blank((header.length - w1) / 2))
        appendln(header)
        append(lines.joinToString(separator = "\n") { "$indent$it" })
    }*/
